import { ref } from 'vue';
import type { IHospitalItem, IHospital } from './types';
import { post } from '../../service/request';

export function userIndexHook() {
	const hospitalListRef = ref<IHospitalItem[]>([]);

	async function initData(pageNo: number, pageSize: number, paging: any) {
		try {
			console.log('pageNo:', pageNo, 'pageSize:', pageSize);
			const hospital = await post<IHospital>('/Hospital/list', {
				page: pageNo,
				limit: pageSize,
				code_province: 110000,
			});

			// 死循环报错
			uni.$emit('z-paging-complete-emit', hospital.list);

			// @ts-ignore
			// paging.value.complete(hospital.list);
			// TypeError: vue.undefined is not a function. (In 'vue.undefined(_ctx.wxsPropType)', 'vue.undefined' is undefined) __ERROR

		} catch (e) {
			// @ts-ignore
			paging.value.complete(false);
		}
	}

	function onHospitalTap(item: IHospitalItem) {
		console.log('点击了医院:', item.hospital_name);
		uni.navigateTo({
			url: '/pages/hospital-details/hospital-details' + uni.$u.queryParams(item),
		});
	}

	return { hospitalListRef, initData, onHospitalTap };
}
